package com.mapbar.pay.action;

import com.mapbar.pay.entity.ActivationState;
import com.mapbar.pay.service.DeviceService;
import com.opensymphony.xwork2.ActionContext;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
/**
 * 处理获取激活码页面的提交
 * @date  2010-3-5  16:10:25
 * @author fengbj
 */
public class GetActivationCodeProcessAction extends BaseAction{

    public Logger logger = Logger.getLogger(this.getClass());//日志对象
    private String serial;
    private String deviceId;
    private String deviceIdTwo;
    private DeviceService deviceService;
    private ActivationState aState;

    @Override
    public String execute() {

        //向后传值
        //ServletActionContext.getRequest().setAttribute("serial", serial);
        
         ActionContext.getContext().getParameters().put("serial", serial);
         ActivationState aStateTemp  = deviceService.createActivation(serial, deviceId);
         if(aStateTemp != null){
            //获取激活码成功
             logger.info("获取激活码成功,[" + aStateTemp + "]");
             if(aStateTemp.isIsSerial()){
                 //合法序列号
                 //是否已经激活
                if(aStateTemp.isIsActive()){
                    //设备码是否符合规范
                    if(aStateTemp.isIsDeviceId()){
                        logger.info("获取激活码完成!");
                        this.aState = aStateTemp;
                        aStateTemp = null;
                        return SUCCESS;
                    }else{
                        logger.info("获取激活码完成,您输入的序列号和激活码不匹配");
                        this.addFieldError("serial", "您输入的序列号和激活码不匹配");
                        return "input";
                    }
                }else{
                    logger.error("返回的激活码显示没有激活 , [" + aStateTemp +"]");
                    this.addFieldError("fail", "激活失败");
                    return "input";
                }
             }else{
                logger.info("服务器上没有该序列号,[" + aStateTemp + "]");
                this.addFieldError("serial", "您输入的序列号不存在");
             }
             return "input";
         }else{
            //获取激活码失败
             logger.info("获取激活码失败,[serial=" + aStateTemp + ",deviceId=" + deviceId + "]");

             this.addFieldError("fail", "获取激活码失败");
             return "input";
         }
        //return super.execute();

    }

    @Override
    public void validate() {

        logger.info("用户获取激活码验证：[serial=" + serial + ",deviceId="+deviceId + "]");
         if(deviceId == null || "".equals(deviceId)){
            this.addFieldError("deviceId", "设备码不可为空");
            return;
        }
         if(deviceIdTwo == null || "".equals(deviceIdTwo)){
            this.addFieldError("deviceIdTwo", "需确认设备码");
            return;
        }
         if(!deviceId.equals(deviceIdTwo)){
            this.addFieldError("deviceIdTwo", "两次输入设备码不一致");
            return;
        }
         if(serial == null){
             
            this.addFieldError("serial", "序列号不可为空");
            return;
        }else{
            serial = serial.replaceAll(", *", "-");
            if(serial == null || "".equals(serial) || ",,,".equals(serial)){
                this.addFieldError("serial", "序列号不可为空");
                return;
            }
        }
    }



    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public String getDeviceIdTwo() {
        return deviceIdTwo;
    }

    public void setDeviceIdTwo(String deviceIdTwo) {
        this.deviceIdTwo = deviceIdTwo;
    }

    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    public ActivationState getaState() {
        return aState;
    }

 


   

    


}
